<template>
  <!-- 用户信息列表 -->
  <div class="app-container">
    <div class="form-container">
      <el-form ref="queryForm" :inline="true" label-width="80px" :model="filters" class="selectFrom">
        <el-form-item label="用户名称" prop="name">
          <el-input v-model="filters.name" size="small" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="用户类型" prop="surveyType">
          <el-select v-model="filters.surveyType" clearable size="small">
            <el-option
              v-for="dict in UserTypeList"
              :key="dict.typeNum"
              :label="dict.typeName"
              :value="dict.typeNum"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否加入" prop="userStatus">
          <el-select v-model="filters.userStatus" clearable size="small">
            <el-option
              v-for="dict in joinOrNot"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所在区域" prop="province">
          <el-cascader v-model="currentProvince" size="small" placeholder="请选择所在区域" :props="cityNormalizer" :options="cityOptions" clearable style="width: 100%" @change="changeCitySelect">
            <template slot-scope="{ data }">
              <span>{{ data.label }}</span>
            </template>
          </el-cascader>
        </el-form-item>
        <!-- 高级设置一栏 start -->
        <el-form-item>
          <slot name="label">
            <i class="highLevelStyle" :class="highLevelSet===true?'el-icon-arrow-up':'el-icon-arrow-down'" @click="highLevelSet = !highLevelSet" />
          </slot>
        </el-form-item>
        <el-row v-if="highLevelSet">
          <el-form-item label="" prop="status">
            <template slot="label">
              <span class="labelTitle" v-html="`状&emsp;&emsp;态`" />
            </template>
            <el-select v-model="filters.status" clearable size="small">
              <el-option
                v-for="dict in statusList"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              size="small"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :clearable="false"
            />
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <!--操作按钮-->
    <div class="btns-group">
      <el-button v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery">搜索</el-button>
      <el-button v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery">重置</el-button>
    </div>
    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="tableLoading"
      stripe
      class="table-list"
      style="width: 100%;"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      :data="tableData"
      :row-style="rowClass"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column type="index" label="序号" width="51" align="center" />
      <el-table-column prop="name" label="用户名称" min-width="140" align="center" />
      <el-table-column prop="sex" :formatter="(row) => formatterPersonSex(row.sex)" label="性别" min-width="60" align="center" />
      <el-table-column prop="phone" label="手机号" min-width="120" align="center" />
      <el-table-column prop="idCard" label="身份证号" min-width="140" align="center" />
      <el-table-column prop="surveyType" :formatter="(row) => formatterPersonSupperType(row.surveyType)" label="用户类型" min-width="100" align="center" />
      <el-table-column prop="enterpriseName" label="所属企业" min-width="200" align="center" />
      <el-table-column prop="enterpriseName" :formatter="(row) => getRegionCode(row.province,row.city,row.county)" label="地区" min-width="200" align="center" />
      <el-table-column prop="status" label="状态" :formatter="(row) => formatterPersonStatus(row.status)" min-width="80" align="center" />
    </el-table>
    <!-- 表格展示end-->
    <!-- 分页-->
    <pagination
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
    <!-- 用户详情弹框 -->
    <detailDialog ref="detailDialog" />
  </div>
</template>

<script>
// api
import { listRegionTree } from '@/api/common/area.js'
import { personTypeListNoPermission } from '@/api/superviseBaseInfo/typeManage/personnelType/index'
import { listSelectPeopleInformation } from '@/api/superviseBaseInfo/userInfoList'
// 详情弹框
import detailDialog from './components/detailDialog'
import { mapState } from 'vuex'
export default {
  name: 'PersonnelType',
  components: {
    detailDialog
  },
  data() {
    return {
      tableLoading: false, // 表格加载
      highLevelSet: false, // 高级搜索
      // 筛选条件
      filters: {
        pageNum: 1,
        pageSize: 20,
        name: '',
        surveyType: '',
        province: '',
        city: '',
        county: '',
        status: '',
        userStatus: ''
      },
      // 日期范围
      dateRange: [],
      // 选择的城市
      currentProvince: '',
      cityOptions: [], // 城市数据
      cityNormalizer: {
        value: 'value',
        label: 'label',
        children: 'children'
      },
      UserTypeList: [], // 用户类型
      joinOrNot: [
        {
          dictValue: '',
          dictLabel: '全部'
        },
        {
          dictValue: '0',
          dictLabel: '加入'
        },
        {
          dictValue: '1',
          dictLabel: '未加入'
        }
      ], // 是否加入企业
      statusList: [
        {
          dictValue: '',
          dictLabel: '全部'
        },
        {
          dictValue: '0',
          dictLabel: '正常'
        },
        {
          dictValue: '1',
          dictLabel: '注销'
        }
      ], // 状态
      // 分页总条数
      total: 0,
      // 非单个禁用
      single: true,
      isDetail: true,
      // 非多个禁用
      multiple: true,
      // 选中表格对话框的存储的值
      multipleSelection: [],
      // 高亮样式
      selectRow: [],
      // 表格数据
      tableData: []
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView
    })
  },
  mounted() {
    // 获取列表数据
    this.getList()
    this.getRegionTree()
    this.getSupperType()
  },
  methods: {
    // 获取区域树
    async getRegionTree() {
      const { data } = await listRegionTree({ regionCode: '' })
      if (data) this.cityOptions = data
    },
    // 所属区域
    changeCitySelect(regionCode) {
      const [province, city, county] = regionCode
      this.filters.province = province // 省
      this.filters.city = city// 市
      this.filters.county = county // 区/县
    },
    // 获取用户类型
    async getSupperType() {
      const { data: { rows }} = await personTypeListNoPermission()
      this.UserTypeList = [{ typeNum: '', typeName: '全部' }, ...rows]
    },
    // 回显用户类型
    formatterPersonSupperType(userType) {
      if (userType) {
        const items = this.UserTypeList.filter(item => {
          return item.typeNum === userType
        })
        return items.length > 0 ? items[0].typeName : '-'
      }
    },
    // 获取数据
    async getList() {
      this.tableLoading = true
      try {
        const { data: { rows, total }} = await listSelectPeopleInformation(this.changeDateRange(this.filters, this.dateRange))
        this.tableData = rows
        this.total = total
        this.tableLoading = false
        // this.addTaleData()
      } catch {
        this.tableLoading = false
      }
    },
    // 回显地区
    getRegionCode(province, city, county) {
      if (!county) return
      if (this.cityOptions.length > 0) {
        const sheng = this.cityOptions.find(sheng => sheng.value === province)
        const shi = sheng.children.find(shi => shi.value === city)
        const xian = shi.children.find(xian => xian.value === county)
        const currentProvince = (sheng.label + shi.label + xian.label) || ''
        return currentProvince
      }
    },
    // 回显状态
    formatterPersonStatus(status) {
      if (status) {
        const items = this.statusList.filter(item => {
          return item.dictValue === status
        })
        return items.length > 0 ? items[0].dictLabel : '-'
      }
    },
    // 回显性别
    formatterPersonSex(sex) {
      if (sex) {
        return sex === '0' ? '男' : '女'
      }
    },
    // 补空白行
    addTaleData() {
      if (this.tableData.length < this.filters.pageSize) {
        var arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    /* 表格选中行事件 start */
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      // 将选中的数组赋给常量 selectData
      const selectData = this.multipleSelection
      // 调用表格的方法将所有已选的都取消
      this.$refs.multipleTable.clearSelection()
      // 判断选中的数组个数是不是等于 1
      if (selectData.length === 1) {
        // 循环判断有没有和当前行的相同的
        selectData.forEach((item) => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        // 不等于1时
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }

      this.selectRow = []
      this.multipleSelection = val
      this.isDetail = val !== 1
      this.single = val.length === 0 || val[0].isFixed === '0'
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    /* 表格选中行事件 end */

    /* 按钮 start */
    // 查询
    handleQuery() {
      this.getList()
    },
    // 重置搜索条件
    resetQuery() {
      this.filters.province = ''
      this.filters.city = ''
      this.filters.county = ''
      this.currentProvince = ''
      this.dateRange = []
      this.resetForm('queryForm')
      this.getList()
    }
    /* 按钮 end */
  }
}
</script>

